import 'package:flutter/material.dart';
import '../widgets/modern_sidebar.dart';
import '../widgets/note_list_view.dart';
import '../theme/app_theme.dart';

/**
 * 主界面屏幕
 * 包含侧边栏和笔记列表视图的整体布局
 */
class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  String? _selectedCategory;
  String? _selectedFolder;

  /**
   * 处理分类选择
   */
  void _onCategorySelected(String category) {
    setState(() {
      _selectedCategory = category;
      _selectedFolder = null; // 清除文件夹选择
    });
  }

  /**
   * 处理文件夹选择
   */
  void _onFolderSelected(String folderId) {
    setState(() {
      _selectedFolder = folderId;
      _selectedCategory = null; // 清除分类选择
    });
  }

  /**
   * 获取当前标题
   */
  String _getCurrentTitle() {
    if (_selectedFolder != null) {
      return '文件夹笔记';
    }
    return _selectedCategory ?? '全部笔记';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppTheme.backgroundPrimary,
      body: Row(
        children: [
          // 侧边栏
          ModernSidebar(
            onCategorySelected: _onCategorySelected,
            onFolderSelected: _onFolderSelected,
          ),
          // 主内容区域
          Expanded(
            child: Container(
              decoration: const BoxDecoration(
                color: AppTheme.backgroundPrimary,
              ),
              child: Column(
                children: [
                  // 顶部标题栏
                  _buildTopBar(),
                  // 笔记列表
                  Expanded(
                    child: NoteListView(
                      selectedFolderId: _selectedFolder,
                      searchQuery: null,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  /**
   * 构建顶部标题栏
   */
  Widget _buildTopBar() {
    return Container(
      height: 60,
      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
      decoration: const BoxDecoration(
        color: AppTheme.backgroundPrimary,
        border: Border(
          bottom: BorderSide(color: AppTheme.borderLight, width: 1),
        ),
      ),
      child: Row(
        children: [
          Text(
            _getCurrentTitle(),
            style: const TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.w600,
              color: AppTheme.textPrimary,
            ),
          ),
          const Spacer(),
          // 工具栏按钮
          _buildToolbarButton(
            icon: Icons.view_list,
            tooltip: '列表视图',
            onPressed: () {},
          ),
          const SizedBox(width: 8),
          _buildToolbarButton(
            icon: Icons.view_module,
            tooltip: '卡片视图',
            onPressed: () {},
          ),
          const SizedBox(width: 8),
          _buildToolbarButton(
            icon: Icons.sort,
            tooltip: '排序',
            onPressed: () {},
          ),
          const SizedBox(width: 16),
          // 新建笔记按钮
          ElevatedButton.icon(
            onPressed: () {},
            icon: const Icon(Icons.add, size: 18),
            label: const Text('新建笔记'),
            style: ElevatedButton.styleFrom(
              backgroundColor: AppTheme.primaryColor,
              foregroundColor: Colors.white,
              elevation: 0,
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /**
   * 构建工具栏按钮
   */
  Widget _buildToolbarButton({
    required IconData icon,
    required String tooltip,
    required VoidCallback onPressed,
  }) {
    return Tooltip(
      message: tooltip,
      child: IconButton(
        onPressed: onPressed,
        icon: Icon(icon, size: 18),
        style: IconButton.styleFrom(
          foregroundColor: AppTheme.textSecondary,
          backgroundColor: Colors.transparent,
          padding: const EdgeInsets.all(8),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(6),
          ),
        ),
      ),
    );
  }
}